package date_demo;

/**
 * @author ChenYongHao
 * @date 2023/5/4 10:12
 * @desc 描述：用于计算两个“日期”间隔，可以获取年、月、天、总月
 */

import java.time.LocalDate;
import java.time.Period;

public class Period_Demo {
    public static void main(String[] args) {
        LocalDate ld1 = LocalDate.now();
        LocalDate ld2 = LocalDate.of(2020, 1, 21);

        Period period = Period.between(ld2, ld1);
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
        long totalMonths = period.toTotalMonths();

        System.out.println("“日期”间隔 --------年 "+years);// 年
        System.out.println("“日期”间隔 --------月 "+months);// 月
        System.out.println("“日期”间隔 --------天 "+days);// 天
        System.out.println("“日期”间隔 --------总月 "+totalMonths);// 总月
    }
}
